package com.money.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    EditText rmb;
    TextView result;
    float dollarRate=0.1503f;
    float euroRate=0.1266f;
    float wonRate=170.2708f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rmb=(EditText)findViewById(R.id.rmb);
        result=(TextView)findViewById(R.id.rmb);

    }

    public void click(View btn){
        float r=0.0f;
        if (btn.getId()==R.id.btn_dollar){
            r=dollarRate;
        }else if (btn.getId()==R.id.btn_euro)
            r=euroRate;
        else {
            r=wonRate;
        }
        //获取用户输入-
        String str = rmb.getText().toString();
        if (str!=null&&str.length()>0){
            r = r*Float.parseFloat(str);
            result.setText(String.format("%.2f",r));
        }
        else {
            Toast.makeText(this,"请输入人民币金额",Toast.LENGTH_SHORT).show();
        }


    }
    public void openConfig(View btn){
        Intent intent = new Intent(this,Config.class);
        intent.putExtra("dollar_key",dollarRate);
        intent.putExtra("euro_key",euroRate);
        intent.putExtra("won_key",wonRate);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        if (requestCode==1&&resultCode==1){
            Bundle bundle=data.getExtras();
            dollarRate=bundle.getFloat("get1",0.0f);
            euroRate=bundle.getFloat("get2",0.0f);
            wonRate=bundle.getFloat("get3",0.0f);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
